package com.ciwong.epaper.modules.me.adapter;

import java.util.ArrayList;
import java.util.List;


import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * Created by 纵约长 on 2017/6/2.
 */
public abstract class BaseCommonAdapter<T> extends BaseAdapter {

	protected List<T> dataList;

	public BaseCommonAdapter(List<T> dataList) {
		this.dataList = dataList;

	}

	public List<T> getData(){
		if(dataList==null){
			dataList=new ArrayList<T>();
		}
		return dataList;
	}
	@Override
	public int getCount() {
		return dataList == null ? 0 : dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList == null ? null : dataList.get(position);
	}
	

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Object holder;
		if (convertView == null) {
			convertView = View.inflate(parent.getContext(), getItemResId(position), null);

			holder = createViewHolder(convertView, position);
			convertView.setTag(holder);
		} else {
				holder=convertView.getTag();
		}
			T data  = dataList.get(position);
			showData(data,holder,position);
		return convertView;
	}

	/**实现item布局	 */
	public abstract int getItemResId(int position);
	
	
	/**子类实现此方法返回一个holder给父类*/		
	public abstract Object createViewHolder(View convertView, int position);
	/**显示数据*/
	public abstract void showData(T bean, Object holder, int position);

}
